package com.cn.wanxi.servlet.back.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        0.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        1.得到前端传递过来的值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String id = req.getParameter("id");
//        2.封装对象
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setSex(sex);
        userModel.setPassword(password);
        userModel.setPhone(phone);
        userModel.setAddress(address);
        userModel.setEmail(email);
        userModel.setId(Integer.parseInt(id));
//        3.调用服务逻辑层
        UserService userService = new UserServiceImpl();
//        4.得到返回的数据
        ResultModel resultModel = userService.update(userModel);
//        5.将返回的数据传递到前端(统一应该用json对象进行传递)
//        resp.getWriter().println(result);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("data", result);
//        jsonObject.put("code", 0);
//        jsonObject.put("message", "增加成功");
//        jsonObject.put("total", result);//影响的条数
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
